import { Frame } from '@/frame';
import UnitInfoModel from '../Model/UnitInfoModel';
import UICommonTip from './UICommonTip';
import ColorStr from '@/ColorStr';
import { Trigger } from '@/trigger';
import SelectUtil from '@/SelectUtil';
import UnitCommon from 'code/Core/Common/UnitCommon';
import { formatBigValue } from '../common';

const toggleIconNormal = 'UI\\Widgets\\Console\\Human\\infocard-neutral-armor-small.blp';
const toggleIconPlus = 'UI\\Widgets\\Console\\Human\\infocard-neutral-armor-medium.blp';

export default class UnitInfoView {
    private model: UnitInfoModel;
    /**
     * 详细根面板
     */
    private detailPanel: Frame;
    /** 简洁根面板 */
    private simplePanel: Frame;
    /** 详细信息面板 ， 以 config 的key 为 key */
    detailFrames: { [index: string]: Frame } = {};
    simpleFrames: { [index: string]: Frame } = {};

    /** 信息面板的数据存储，以 frame 的 handle 为key，存储对应的提示信息 */
    framesTipData: { [index: string]: UnitStateModel } = {};
    /** 单位头像的生命魔法等ui */
    unitFrames: { [index: string]: Frame } = {};
    /** 切换模式按钮组 */
    toggleButs: Frame[] = [];
    /** 显示模式 */
    mode: 'simple' | 'detail' = 'simple';
    constructor() {
        this.model = new UnitInfoModel();
        this.drawUI();
        this.showHideRootPanel();
        this.start();
    }
    start() {
        let t = new Trigger();
        t.registerTimerEvent(0.1, true);
        t.addAction(this.refreshUnitInfoUI);
    }
    refreshUnitInfoUI(this: void) {
        let tar = SelectUtil.getRealSelectUnit();
        let _this = UIManager.UnitInfo;
        if (tar == null) {
            _this.detailPanel.visible = false;
            _this.simplePanel.visible = false;
            return;
        }
        _this.showHideRootPanel();
        if (_this.mode == 'simple') {
            _this.refreshSimpleUI(tar);
        } else {
            _this.refreshDetailUI(tar);
        }
        _this.refreshUnitUI(tar);
    }
    refreshUnitUI(tar: unit) {
        let f = this.unitFrames;
        f.生命.setText(formatBigValue(GetUnitState(tar, UNIT_STATE_LIFE)));
        f.生命条.setSize(0.12 * GetUnitLifePercent(tar) * 0.01, 0.012);
        f.魔法.setText(formatBigValue(GetUnitState(tar, UNIT_STATE_MANA)));
        f.魔法条.setSize(0.12 * GetUnitManaPercent(tar) * 0.01, 0.012);
        f.生命回复.setText(UnitCommon.取生命回复(tar).toString());
        f.魔法回复.setText(UnitCommon.取法力回复(tar).toString());
        f.护盾.setText(`护盾:${R2I(ShieldSystem.获取总护盾值(tar))}`);
    }
    refreshDetailUI(tar: unit) {
        let f = this.detailFrames;
        // let 单位 = Unit.fromHandle(tar);
        f.体质.setText(I2S(R2I(UnitCommon.取总耐力(tar))));
        f.力量.setText(I2S(R2I(UnitCommon.取总力量(tar))));
        f.敏捷.setText(I2S(R2I(UnitCommon.取总敏捷(tar))));
        f.智力.setText(I2S(R2I(UnitCommon.取总智力(tar))));
        f.精神.setText(I2S(R2I(UnitCommon.取总精神(tar))));
        f.移动.setText(I2S(UnitCommon.取总移动速度(tar)));
        f.攻击.setText(UnitCommon.取固定攻击(tar).toString());
        let phc = UnitCommon.取物理暴击率(tar);
        phc = phc > 1 ? 1 : phc;
        f.物理暴击.setText(`${R2I(phc * 100)}%`);
        f.物理暴击伤害.setText(`${R2I(UnitCommon.取物理暴击伤害(tar) * 100)}%`);
        f.护甲.setText(R2I(UnitCommon.取总护甲(tar)).toString());
        f.法强.setText(UnitCommon.取法强(tar).toString());
        let mgc = UnitCommon.取法术暴击率(tar);
        mgc = mgc > 1 ? 1 : mgc;
        f.法术暴击.setText(`${R2I(mgc * 100)}%`);
        f.法术暴击伤害.setText(`${R2I(UnitCommon.取法术暴击伤害(tar) * 100)}%`);
        f.魔抗.setText(R2I(UnitCommon.取总魔抗(tar)).toString());
        f.冷却.setText(`${(1 - UnitCommon.取冷却降低(tar)) * 100}%`);
        f.治疗.setText(`${UnitCommon.取治疗增强(tar) * 100}%`);
        let sb = UnitCommon.取总闪避(tar);
        sb = sb > 1 ? 1 : sb;
        f.闪避.setText(`${R2I(sb * 100)}%`);
        f.射程.setText(I2S(GetUnitState(tar, UnitStateDamageRange)));
    }
    refreshSimpleUI(tar: unit) {
        let panels = this.simpleFrames;
        panels.攻击.setText(UnitCommon.取固定攻击(tar).toString());
        panels.法强.setText(UnitCommon.取法强(tar).toString());
        panels.护甲.setText(R2I(UnitCommon.取总护甲(tar)).toString());
        panels.魔抗.setText(R2I(UnitCommon.取总魔抗(tar)).toString());
        panels.体质.setText(I2S(R2I(UnitCommon.取总耐力(tar))));
        panels.力量.setText(I2S(R2I(UnitCommon.取总力量(tar))));
        panels.敏捷.setText(I2S(R2I(UnitCommon.取总敏捷(tar))));
        panels.智力.setText(I2S(R2I(UnitCommon.取总智力(tar))));
        panels.精神.setText(I2S(R2I(UnitCommon.取总精神(tar))));
    }
    /** 画所有的ui */
    drawUI() {
        this.drawUnitFrames();
        this.drawDetailUI();
        this.drawSimplelUI();
        this.drawToggleButton();
    }
    /** 切换用的按钮 */
    drawToggleButton() {
        let bg = (this.toggleButs[0] = Frame.createBackDrop());
        let but = (this.toggleButs[1] = Frame.createGLUEBUTTON());
        bg.setSize(0.025, 0.025).setTexture(toggleIconNormal).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.155, 0.002);
        but.setSize(0.025, 0.025).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.155, 0.002);
        but.setOnMouseEnter(UnitInfoView.showToggleInfo);
        but.addOnMouseLeave(UnitInfoView.hideToggleInfo);
        but.setOnClick(UnitInfoView.toggleMode);
    }
    drawUnitFrames() {
        let root = DzGetGameUI();
        // 数值根面板
        let rootPanel = Frame.createFrame(root).setSize(0.1, 0.04).setPoint(FRAMEPOINT_BOTTOMLEFT, root, FRAMEPOINT_BOTTOMLEFT, 0.185, 0);
        // shieldBar
        let 护盾背景 = Frame.createBackDrop(rootPanel.current)
            .setTexture('assets\\status\\statu-bar-bg.blp')
            .setSize(0.12, 0.012)
            .setPoint(FRAMEPOINT_TOPLEFT, rootPanel.current, FRAMEPOINT_TOPLEFT, 0, 0);
        let 护盾文本 = Frame.createTEXT(护盾背景.current)
            .setSize(0.12, 0.012)
            .setPoint(FRAMEPOINT_TOPLEFT, rootPanel.current, FRAMEPOINT_TOPLEFT)
            .setText('护盾:0')
            .setTextColor(0xffeeee00)
            .setFont(0.01, 'fonts\\fonts.ttf');
        护盾文本.setTextAlignment(TEXTALIGN_CENTER);
        this.unitFrames['护盾'] = 护盾文本;
        // lifeBar
        let 生命条背景 = Frame.createBackDrop(rootPanel.current).setTexture('assets\\bg_64x64.blp').setSize(0.12, 0.012).setPoint(FRAMEPOINT_TOPLEFT, rootPanel.current, FRAMEPOINT_TOPLEFT, 0, -0.013);
        let 生命条 = Frame.createBackDrop(生命条背景.current).setTexture('assets\\status\\hp-bar.blp').setSize(0.12, 0.012).setPoint(FRAMEPOINT_TOPLEFT, 生命条背景.current, FRAMEPOINT_TOPLEFT, 0, 0);
        this.unitFrames['生命条'] = 生命条;
        let 生命文本 = Frame.createTEXT(生命条背景.current)
            .setSize(0.12, 0.012)
            .setPoint(FRAMEPOINT_TOPLEFT, 生命条背景.current, FRAMEPOINT_TOPLEFT)
            .setText('0/0')
            .setTextColor(0xff00ee00)
            .setFont(0.01, 'fonts\\fonts.ttf');
        生命文本.setTextAlignment(TEXTALIGN_CENTER);
        this.unitFrames['生命'] = 生命文本;
        let 生命回复文本 = Frame.createTEXT(生命条背景.current)
            .setSize(0.12, 0.012)
            .setPoint(FRAMEPOINT_TOPLEFT, 生命条背景.current, FRAMEPOINT_TOPLEFT, 0, 0.0)
            .setText('0')
            .setTextColor(0xff00ee00)
            .setFont(0.01, 'fonts\\fonts.ttf');
        生命回复文本.setTextAlignment(TEXTALIGN_RIGHT);
        this.unitFrames['生命回复'] = 生命回复文本;
        生命回复文本.visible = false;

        // manaBar
        let 魔法背景 = Frame.createBackDrop(rootPanel.current).setTexture('assets\\bg_64x64.blp').setSize(0.12, 0.012).setPoint(FRAMEPOINT_TOPLEFT, rootPanel.current, FRAMEPOINT_TOPLEFT, 0, -0.026);
        let 魔法条 = Frame.createBackDrop(魔法背景.current).setTexture('assets\\status\\mp-bar.blp').setSize(0.12, 0.012).setPoint(FRAMEPOINT_TOPLEFT, 魔法背景.current, FRAMEPOINT_TOPLEFT, 0, 0);
        this.unitFrames['魔法条'] = 魔法条;
        let 魔法文本 = Frame.createTEXT(魔法背景.current)
            .setSize(0.12, 0.012)
            .setPoint(FRAMEPOINT_TOPLEFT, 魔法背景.current, FRAMEPOINT_TOPLEFT)
            .setText('0/0')
            .setTextColor(0xff00eeee)
            .setFont(0.01, 'fonts\\fonts.ttf');
        魔法文本.setTextAlignment(TEXTALIGN_CENTER);
        this.unitFrames['魔法'] = 魔法文本;
        let 魔法回复文本 = Frame.createTEXT(魔法背景.current)
            .setSize(0.12, 0.012)
            .setPoint(FRAMEPOINT_TOPLEFT, 魔法背景.current, FRAMEPOINT_TOPLEFT)
            .setText('0')
            .setTextColor(0xff00eeee)
            .setFont(0.01, 'fonts\\fonts.ttf');
        魔法回复文本.setTextAlignment(TEXTALIGN_RIGHT);
        this.unitFrames['魔法回复'] = 魔法回复文本;
        魔法回复文本.visible = false;

        let 显示区域 = Frame.createTEXT();
        显示区域.setSize(0.12, 0.03);
        显示区域.setPoint(FRAMEPOINT_TOPLEFT, rootPanel.current, FRAMEPOINT_TOPLEFT, 0, -0.013);
        显示区域.setOnMouseEnter(UnitInfoView.showRegen);
        显示区域.setOnMouseLeave(UnitInfoView.hideRegen);
    }
    drawSimplelUI() {
        let root = (this.simplePanel = Frame.createFrame(DzGetGameUI()).setSize(0.2, 0.08).setPoint(FRAMEPOINT_BOTTOM, DzGetGameUI(), FRAMEPOINT_BOTTOM, 0, 0));
        // 图标部分与不变的文本部分
        // 攻击图标
        Frame.createBackDrop(root.current).setSize(0.025, 0.025).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.31, 0.052).setTexture('UI\\Widgets\\Console\\Human\\infocard-neutral-attack-hero.blp');
        // 防御图标
        Frame.createBackDrop(root.current).setSize(0.025, 0.025).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.31, 0.02).setTexture('UI\\Widgets\\Console\\Human\\infocard-neutral-armor-small.blp');
        // 属性图标
        Frame.createBackDrop(root.current).setSize(0.025, 0.025).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.4, 0.04).setTexture('UI\\Widgets\\Console\\Human\\infocard-heroattributes-str.blp');
        // 属性文本
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.335, 0.065)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.red + '攻击:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.335, 0.05)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.blue + '法强:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.335, 0.03)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.green + '护甲:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.335, 0.015)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.purple + '魔抗:');
        // 五维
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.425, 0.065)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.orange + '体质:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.425, 0.05)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.red + '力量:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.425, 0.035)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.green + '敏捷:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.425, 0.02)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.purple + '精神:');
        Frame.createTEXT(root.current)
            .setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.425, 0.005)
            .setSize(0.025, 0.014)
            .setFont(0.01, 'fonts\\fonts.ttf')
            .setText(ColorStr.blue + '智力:');
        // values
        this.simpleFrames['攻击'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.357, 0.065).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['法强'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.357, 0.05).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['护甲'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.357, 0.03).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['魔抗'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.357, 0.015).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        //
        this.simpleFrames['体质'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.447, 0.065).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['力量'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.447, 0.05).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['敏捷'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.447, 0.035).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['精神'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.447, 0.02).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
        this.simpleFrames['智力'] = Frame.createTEXT(root.current).setAbsPoint(FRAMEPOINT_BOTTOMLEFT, 0.447, 0.005).setSize(0.06, 0.014).setFont(0.01, 'fonts\\fonts.ttf').setText('0');
    }
    drawDetailUI() {
        let root = DzGetGameUI();
        let _this = this;
        // 创建根面板
        _this.detailPanel = Frame.createFrame(root).setSize(0.2, 0.08).setPoint(FRAMEPOINT_BOTTOM, root, FRAMEPOINT_BOTTOM, 0, 0);
        let { attributes, status } = _this.model.config;
        // 画五维界面
        for (let item of attributes) {
            let f = Frame.createFrame(_this.detailPanel.current).setSize(item.width, item.height).setPoint(FRAMEPOINT_TOPLEFT, _this.detailPanel.current, FRAMEPOINT_TOPLEFT, item.x, item.y);
            // 图标
            Frame.createBackDrop(f.current).setPoint(FRAMEPOINT_TOPLEFT, f.current, FRAMEPOINT_TOPLEFT).setSize(0.015, 0.015).setTexture(item.icon);
            // 文本
            let text = Frame.createTEXT(f.current).setSize(0.03, 0.015).setPoint(FRAMEPOINT_TOPLEFT, f.current, FRAMEPOINT_TOPLEFT, 0.015).setText('0').setFont(0.012, 'fonts\\fonts.ttf');
            text.setTextAlignment(TEXTALIGN_LEFT);
            let tipArea = Frame.createTEXT(f.current);
            tipArea.setSize(0.04, 0.015).setPoint(FRAMEPOINT_TOPLEFT, f.current, FRAMEPOINT_TOPLEFT);
            tipArea.setOnMouseEnter(UnitInfoView.showTip);
            tipArea.setOnMouseLeave(UnitInfoView.hideTip);
            _this.detailFrames[item.key] = text;
            _this.framesTipData['f' + tipArea.current] = item;
        }

        // 详细状态面板
        for (let item of status) {
            let f = Frame.createFrame(_this.detailPanel.current).setSize(item.width, item.height).setPoint(FRAMEPOINT_TOPLEFT, _this.detailPanel.current, FRAMEPOINT_TOPLEFT, item.x, item.y);
            // 图标
            Frame.createBackDrop(f.current).setPoint(FRAMEPOINT_TOPRIGHT, f.current, FRAMEPOINT_TOPRIGHT).setSize(0.02, 0.02).setTexture(item.icon);
            // 文本
            let text = Frame.createTEXT(f.current).setSize(0.038, 0.02).setPoint(FRAMEPOINT_TOPRIGHT, f.current, FRAMEPOINT_TOPRIGHT, -0.0205).setText('0').setFont(0.012, 'fonts\\fonts.ttf');
            let tipArea = Frame.createTEXT(f.current);
            tipArea.setSize(item.width, item.height).setPoint(FRAMEPOINT_TOPRIGHT, f.current, FRAMEPOINT_TOPRIGHT);
            tipArea.setOnMouseEnter(UnitInfoView.showTip);
            tipArea.setOnMouseLeave(UnitInfoView.hideTip);
            text.setTextAlignment(TEXTALIGN_RIGHT);
            _this.detailFrames[item.key] = text;
            _this.framesTipData['f' + tipArea.current] = item;
        }
    }
    static showTip(this: void) {
        let f = DzGetTriggerUIEventFrame();
        let d = UIManager.UnitInfo.framesTipData['f' + f];
        UIManager.CommonUITip.show(d.tip);
    }
    static hideTip(this: void) {
        UIManager.CommonUITip.hide();
    }
    static showRegen() {
        UIManager.UnitInfo.unitFrames['生命回复'].visible = true;
        UIManager.UnitInfo.unitFrames['魔法回复'].visible = true;
    }
    static hideRegen() {
        UIManager.UnitInfo.unitFrames['生命回复'].visible = false;
        UIManager.UnitInfo.unitFrames['魔法回复'].visible = false;
    }
    static showToggleInfo() {
        UIManager.CommonUITip.show(`切换单位信息显示为${UIManager.UnitInfo.mode == 'simple' ? '详细' : '简洁'}模式`);
    }
    static hideToggleInfo() {
        UIManager.CommonUITip.hide();
    }
    static toggleMode() {
        UIManager.UnitInfo.mode = UIManager.UnitInfo.mode == 'simple' ? 'detail' : 'simple';
        UIManager.UnitInfo.toggleButs[0].setTexture(UIManager.UnitInfo.mode == 'simple' ? toggleIconNormal : toggleIconPlus);

        UIManager.UnitInfo.showHideRootPanel();
    }
    showHideRootPanel() {
        this.detailPanel.visible = this.mode == 'detail';
        this.simplePanel.visible = this.mode == 'simple';
    }
}
